package com.gcorp.idealgas;


import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.Ellipse2D;

import com.gcorp.idealgas.type.MoleculeType;
import com.gcorp.idealgas.type.StandardMoleculeWithRandomSpeed;

/**
 * This class defines the molecule
 * @author Admin
 *
 */
public class Molecule implements Drawable
{

	private myPoint initialPosition;
	
	private myPoint currentPosition;
	
	private myPoint currentCenter;
	
	private MoleculeType type;
	
	private boolean isActing;

	private AnimationTick tick;
	
	private boolean bounced;
	
	private Molecule bouncedWith;
	
	Molecule(float sceneDelta)
	{
	    this.initialPosition = this.currentPosition = this.currentCenter  = new myPoint(100,100);
		this.type = new StandardMoleculeWithRandomSpeed();
		this.isActing = true;
		
		this.tick = new AnimationTick(sceneDelta);
	}
	Molecule(float x, float y, MoleculeType type, float sceneDelta)
	{
		this.initialPosition = this.currentPosition = this.currentCenter = new myPoint(x,y);
		this.type = type;
		this.isActing = true;
		
		this.tick = new AnimationTick(sceneDelta);
		
	}
	public void update()
	{
	    tick.update();
	}
	public void reset()
	{
	    resetTick();
	    initialPosition = currentPosition;
	}
	public void setInitialPosition(float x,float y)
	{
		initialPosition.setX(x);
		initialPosition.setY(y);
	}
	public void setCurrentCenter(myPoint currentCenter)
	{
		this.currentCenter = currentCenter;
	}
	public myPoint getCurrentCenter()
	{
		return currentCenter;
	}
	public boolean isActing()
	{
		return isActing;
	}
	public void setIsActing(boolean isActing)
	{
		this.isActing = isActing;
	}
	public void setInitialPosition(myPoint myPoint)
	{
		setInitialPosition(myPoint.getX(),myPoint.getY());
	}
	
	
	public myPoint getInitialPosition()
	{
		return initialPosition;
	}
	
	public float getMass()
	{
	    return type.getMass();
	}
    
    public Color getColor()
    {
        return type.getColor();
    }
    
    public float getRadius()
    {
        return type.getRadius();
    }
    
    public Vector2 getVelocity()
    {
        return type.getVelocity().getVelocityVector();
    }

    public myPoint getCurrentPosition()
    {
    	return currentPosition;
    }
    public void setCurrentPosition(myPoint currentPosition)
    {
    	this.currentPosition = currentPosition;
    }
    public myPoint move()
    {
    	
        float t = tick.getCurrentTime();
        return new myPoint(initialPosition.getX() + type.getVelocity().getX()*t,initialPosition.getY() + type.getVelocity().getY()*t).sub(initialPosition);
    }
    
    public myPoint move(float additioner)
    {
        
        float t = tick.getCurrentTime() + additioner;
        return new myPoint(initialPosition.getX() + type.getVelocity().getX()*t,initialPosition.getY() + type.getVelocity().getY()*t).sub(initialPosition);
    }

	public void reverseX() 
	{	
	    type.reverseX();
	}
	public void reverseY() 
    {   
        type.reverseY();
    }
	public void setdVx(float vx)
	{
		type.setdVx(vx);
	}
	public void setdVy(float vy)
	{
		type.setdVx(vy);
	}
	public boolean isBounced()
	{
		return bounced;
	}
	public void setBounced(Molecule bouncedWith)
	{
		bounced = true;
		this.bouncedWith = bouncedWith;
	}
	public Molecule getBouncedMolecule()
	{
		return bouncedWith;
	}
	public void setUnbounced()
	{
		bounced = false;
		bouncedWith = null;
	}
	
    @Override
    public void draw(Graphics2D g2d) 
    {
        g2d.setColor(getColor());
        Ellipse2D.Double mol = getMoleculeDrawable();
        setCurrentCenter(new myPoint(mol.getCenterX(),mol.getCenterY()));
        g2d.fill(mol);
    }
    private Ellipse2D.Double getMoleculeDrawable()
    {
        return new Ellipse2D.Double(currentPosition.getX(), currentPosition.getY(), 2*getRadius(), 2*getRadius());
    }
    public void resetTick()
    {
        tick.reset();
    }
}